package com.bridgeintelligent.tag.webserver.security;

import com.bridgeintelligent.tag.constants.Permissions;
import com.bridgeintelligent.tag.utils.SecurityHelper;

import java.util.*;

public class TagSecurityHelper extends SecurityHelper {

    static void setCurrentPermissions(List<String> permissionCodes) {
        List<String> middleList = new ArrayList<>();
        Map<String, List<String>> map = createMap();
        permissionCodes.forEach(v -> {
            if (map.containsKey(v)) {
                middleList.addAll(map.get(v));
            }
        });
        permissionCodes.addAll(middleList);
        SecurityHelper.currentPermissions(permissionCodes);
    }

    private static Map<String, List<String>> createMap() {
        Map<String, List<String>> middleMap = new HashMap<>();
        for (Permissions permissions : Permissions.values()) {
            if (middleMap.containsKey(permissions.getUrl())) {
                List<String> list = new ArrayList<>(middleMap.get(permissions.getUrl()));
                list.add(permissions.getCode());
                middleMap.put(permissions.getUrl(), list);
            } else {
                middleMap.put(permissions.getUrl(), Collections.singletonList(permissions.getCode()));
            }
        }
        Map<String, List<String>> result = new HashMap<>();
        middleMap.forEach((k, v) -> {
            if (v.size() > 1) {
                v.forEach(listValue -> {
                    result.put(listValue, v);
                });
            }
        });
        return result;
    }

    static void setCurrentUser(Object user) {
        SecurityHelper.currentUser(user);
    }

    protected static void tagperms(Object tagperms) {
        SecurityHelper.tagperms(tagperms);
    }
}
